(function(angular) {
    var app = angular.module('moviecat.movie_list', [
        'ngRoute',
        'moviecat.myjsonp'
    ]);

    // 配置路由
    app.config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/:movieType/:page?', {
            templateUrl: 'movie_list/view.html',
            controller: 'movie_listController'
        });
    }]);

    // 创建控制器
    app.controller('movie_listController', [
        '$scope', '$http', '$routeParams', '$route', 'MyJsonp',
        function($scope, $http, $routeParams, $route, MyJsonp) {
            console.log($routeParams);
            $scope.loading = true;
            $scope.pageSize = 10;
            $scope.nowPage = ($routeParams.page || "1") - 0;
            var start = ($scope.nowPage - 1) * $scope.pageSize;
            MyJsonp.jsonp('http://api.douban.com/v2/movie/' + $routeParams.movieType + '?q=' + $routeParams.q, { start: start, count: $scope.pageSize }, function(data) {
                $scope.data = data;
                $scope.total = data.total;
                $scope.totalPage = Math.ceil(data.total / $scope.pageSize);
                $scope.loading = false;
                $scope.$apply(); //在改变数据模型之后调用,强制angular监视数据模型的改变
            });

            $scope.goPage = function(newPage) {
                // 防止用户恶意点击
                if (newPage < 1 || newPage > $scope.totalPage) {
                    return;
                }
                $route.updateParams({ page: newPage })
            }
        }
    ]);

})(angular)